package edu.nctu.csie.jichang.database.model.cell;

public class DBTable extends AbstractContainer<DBRow>{

	private DBTableHeader header = null;
	private String token = "000000000001".intern();
	
	public DBTable(String pName){
		super(pName);
	}
	
	public DBTable(String pName, DBTableHeader pHeader) {
		this(pName);
		header = pHeader;
	}

	public String getTableName(){
		return getName();
	}
	
	public void setTableName(String pName){
		setName(pName);
	}
	
	public void addRow(DBRow pRow){
		pRow.setName(token);
		addChild(token, pRow);
		jumpNextToken();
	}
	
	public String[] getRowNames(){
		return getAllNames();
	}
	
	public DBRow[] getAllRows(){
		return getChildren().values().toArray(new DBRow[0]);
	}
	
	public DBRow getRow(String pRow){
		return (DBRow)getChild(pRow);
	}

	public void addTableHeader(DBTableHeader pTableHeader) {
		header = pTableHeader;
	}
	
	public DBTableHeader getTableHeader(){
		return header;
	}

	public boolean hasTheSameRow(DBRow pRow) {
		for(DBRow tRows:getAllRows()){
			if(tRows.isSame(pRow)){
				return true;
			}
		}
		return false;
	}

	public void removeRow(String pName) {
		removeChild(pName);
	}
	
	/**
	 * get Next Token
	 * @param pToken Token
	 * @return Next Token
	 */
	private synchronized void jumpNextToken() {
		String tTemp = "000000000000".intern()+(Integer.parseInt(token)+1);
		token = tTemp.substring(tTemp.length()-12);
	}
}
